<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/GaleryItem.interface.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/HumeurDatabase.class.php");
		
	class Shape implements GaleryItem{
		
		var $shapeName;
		var $javascriptFunction;
		var $isActivated;
		
		public function Shape($data){
			if(is_array($data)) $this->create($data);
			elseif(is_string($data)) $this->load($data);
		}
		
		public function load($id){
			$db = new HumeurDatabase();
			$data = mysql_fetch_assoc($db->query("SELECT * FROM shapes WHERE shapeName = '".$id."' LIMIT 1;"));
			$this->create($data);
		}
		
		public function create($array){
			if(isset($array['shapeName'])) $this->shapeName = $array['shapeName'];
			if(isset($array['javascriptFunction'])) $this->javascriptFunction = $array['javascriptFunction'];
			if(isset($array['isActivated'])) $this->isActivated = $array['isActivated'];
		}
		
		public function save(){
			$db = new HumeurDatabase();
			if(mysql_num_rows($db->query("SELECT * FROM shapes WHERE shapeName = '".$this->shapeName."' LIMIT 1;")) > 0) return $this->update();
			else return $this->insert();
		}
		
		public function insert(){
			$db = new HumeurDatabase();
			return $db->query("INSERT INTO shapes VALUES ('".$this->shapeName."','".$this->javascriptFunction."',1);");
		}
		
		public function delete(){
			$db = new HumeurDatabase();
			return $db->query("DELETE FROM shapes WHERE shapeName = '".$this->shapeName."';");
		}
		
		public function update(){
			$db = new HumeurDatabase();
			return $db->query("UPDATE shapes SET javascriptFunction = '".$this->javascriptFunction."', isActivated = '".$this->isActivated."' WHERE shapeName = '".$this->shapeName."';");
		}
		
		public function toggleActivation(){
			$this->isActivated = ($this->isActivated == 1)? 0:1;
		}
		
		public function summary(){
			return '<div id="div'.$this->shapeName.'" class="'.(($this->isActivated == '1')? "activated":"disactivated").' existingColorBloc centered">
				'.$this->shapeName.'<br/>
				<div id="'.$this->shapeName.'"></div>
				<div>
					<span id="'.$this->shapeName.'javascriptFunction" class="">'.$this->javascriptFunction.'</span>
				</div>
				<script>createShape(\''.$this->shapeName.'\',\''.$this->javascriptFunction.'\',0,0,0)</script>
				<a onclick="ajaxLoad(\'makeShapeEditForm\',\'id='.$this->shapeName.'\',makeShapeEdit_response)"><img src="img/editMember.png" alt="Editer"/></a>
				<a onclick="ajaxLoad(\'deleteShape\',\'id='.$this->shapeName.'\',deleteShape_response)">'.(($this->isActivated == 0)? '<img src="img/activeColor.png" alt="Activer"/>':'<img src="img/deleteMember.png" alt="Désactiver"/>').'</a>
			</div>';
		}
		
		public function editForm(){
			return '<div id="div'.$this->shapeName.'" class="existingColorBloc centered edited">
				'.$this->shapeName.'<br/>
				<div id="'.$this->shapeName.'"></div>
				<div>
					<input type="text" onkeyup="createShape(\''.$this->shapeName.'\',document.getElementById(\''.$this->shapeName.'javascriptFunction\').value,0,0,0)" id="'.$this->shapeName.'javascriptFunction" class="" value="'.$this->javascriptFunction.'" onkeyup="createShape(\''.$this->shapeName.'\',\''.$this->javascriptFunction.'\',0,0,0)"/>
				</div>
				<input type="button" value="Modifier" onclick="ajaxLoad(\'editShape\',\'shapeName='.$this->shapeName.'&javascriptFunction=\'+document.getElementById(\''.$this->shapeName.'javascriptFunction\').value+\'&isActivated='.$this->isActivated.'\',editShape_response)" />
			</div>';
		}
		
		public function __toString(){
			return $this->shapeName;
		}
	}
?>